<?php
/**
 * Created by PhpStorm.
 * Author: zhugege
 * Date: 2017/03/08
 * Time: 11:21
 */
namespace App\Http\Admin\Personnel\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Repositories\Train\TrainRepository;
use App\Repositories\Train\TrainApplyRepository;
use App\Repositories\Train\HomeworkSubmitRepository;

class Train extends Model
{

    /**
     * 获取培训列表
     * @author zhugege
     */
    public function getTrainList()
    {
        $pageSize = request()->pageSize;
        $pageCurrent = request()->pageCurrent;
        $condition = [];
        $params = request()->only(['start_time', 'end_time', 'pageSize', 'pageCurrent']);
//        debug($params);
        if ($params['start_time']) {
            if ($params['end_time']) {
                $condition = [
                    ['start_time', '>=', strtotime($params['start_time'])],
                    ['end_time', '<=', strtotime($params['end_time'])]
                ];
            } else {
                $condition = [
                    ['start_time', '>=', strtotime($params['start_time'])]
                ];
            }
        } else {
            if ($params['end_time']) {
                $condition = [
                    ['end_time', '<=', strtotime($params['end_time'])]
                ];
            }
        }

//        debug($condition);

        $train = new TrainRepository();

        $data = $train->getPagingList($params['pageCurrent'], $params['pageSize'], $condition);
        return $data[0];
    }

    /**
     * 添加培训
     * @author zhugege
     * @param $data
     */
    public function addTrain($data)
    {
        $train = new TrainRepository();
        $train->create($data);
        return true;
    }

    /**
     * 获取培训详情
     * @author zhugege
     * @param $id
     * @return mixed
     */
    public function getTrainInfo($id)
    {
        $train = new TrainRepository();
        $trainInfo = $train->find($id);
        $trainInfo['start_time'] = Date('Y-m-d', $trainInfo['start_time']);
        $trainInfo['end_time'] = Date('Y-m-d', $trainInfo['end_time']);
        return $trainInfo;
    }

    /**
     * 编辑培训
     * @author zhugege
     */
    public function updateTrain($id, $attribute)
    {
        $train = new TrainRepository();
        $train->update($id, $attribute);
        return true;
    }

    /**
     * 获取报名列表
     * @author zhugege
     * @param $page
     * @param $page_size
     * @return mixed
     */
    public function getTrainApplyList($page, $page_size)
    {
        $trainApply = new TrainApplyRepository();
        $trainApplyList = $trainApply->getPagingList($page, $page_size);
        return $trainApplyList[0];
    }

    /**
     * 获取报名详情
     * @author zhugege
     * @param $id
     */
    public function getTrainApplyInfo($id)
    {
        $trainApply = new TrainApplyRepository();
        $trainApplyInfo = $trainApply->find($id);
        $trainApplyInfo->apply_time = $trainApplyInfo->created_at->format('Y-m-d');
        return $trainApplyInfo;
    }

    /**
     * 获取作业提交列表
     * @author zhugege
     */
    public function getHomeworkSubmitList($page, $pageSize)
    {
        $homeworkSubmit = new HomeworkSubmitRepository();
        list($homeworkSubmitList, $total) = $homeworkSubmit->getPagingList($page, $pageSize, [], ['userInfo']);
//        list($homeworkSubmitList, $total) = $homeworkSubmit->getPagingList($page, $pageSize);

        foreach ($homeworkSubmitList as $homeworkSubmit) {
            $homeworkSubmit->user_name = !empty($homeworkSubmit->userInfo)
                ? $homeworkSubmit->userInfo->customer_name
                : '';
            $homeworkSubmit->submit_time = $homeworkSubmit->created_at->format("Y-m-d");
        }

        return $homeworkSubmitList;
    }


    /**
     * 获取详情
     * @author zhugege
     * @param $id
     * @return mixed
     */
    public function getHomeworkSubmitInfo($id)
    {
        $homeworkSubmit = new HomeworkSubmitRepository();
//        $homeworkSubmitInfo = $homeworkSubmit->find($id);
        $homeworkSubmitInfo = $homeworkSubmit->find($id, ['userInfo']);
        $homeworkSubmitInfo['submit_time'] = $homeworkSubmitInfo->created_at->format("Y-m-d");
        $homeworkSubmitInfo['user_name'] = !empty($homeworkSubmitInfo->userInfo) ? $homeworkSubmitInfo->userInfo->customer_name : '';
        $homeworkSubmitInfo['homework_images'] = explode(',',$homeworkSubmitInfo['image_url']);
        debug($homeworkSubmitInfo['homework_images']);
        return $homeworkSubmitInfo;
    }
}